package cn.xiyun.xiyuncc.MQ.consumer;

import cn.xiyun.xiyuncc.domain.PayOrder;
import cn.xiyun.xiyuncc.service.IPayOrderService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "Pay_TimeOut_group",
        topic = "OrderTimeOut_topic",
        selectorExpression = "OrderTimeOut_tags",
        messageModel = MessageModel.BROADCASTING
)
public class payTimeOutConsumer implements RocketMQListener<String> {
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(String orderNo) {
        Wrapper<PayOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("order_no", orderNo);
        // 根据订单号查询支付单
        PayOrder payOrder = payOrderService.selectOne(wrapper);
        if (payOrder == null) return;
        // 判断状态,未支付则改为已过期.
        if (payOrder.getPayStatus() == PayOrder.PAY_WAITING) {
            payOrder.setPayStatus(PayOrder.PAY_CANCEL);
            payOrderService.updateById(payOrder);
        }
    }
}
